package guowei.guliproduct.product.controller;

import java.util.Arrays;
import java.util.Map;

import guowei.guliproduct.product.vo.AttrRelationDelVo;
import guowei.guliproduct.product.vo.AttrRespVo;
import guowei.guliproduct.product.vo.Attrvo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import guowei.guliproduct.product.entity.AttrEntity;
import guowei.guliproduct.product.service.AttrService;
import guowei.common.utils.PageUtils;
import guowei.common.utils.R;



/**
 * 商品属性
 *
 * @author guoguo
 * @email 1573746397@qq.com
 * @date 2023-05-21 17:38:07
 */
@Slf4j
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取商品的规格参数属性
     * @param params
     * @param catId
     * @return
     */
    @RequestMapping("/base/list/{catId}")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){

        PageUtils page = attrService.queryPagrByKeys(params,catId);
        return R.ok().put("page", page);
    }

    /**
     * 获取商品系统的销售属性参数
     * @param params
     * @param catId
     * @return
     */
    @RequestMapping("/sale/list/{catId}")
    //@RequiresPermissions("product:attr:list")
    public R salelist(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){

        PageUtils page = attrService.queryPagrByKeysSale(params,catId);
//        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);
        Attrvo attrvo = new Attrvo();
        BeanUtils.copyProperties(attr,attrvo);

        AttrRespVo attrRespVo = attrService.getByVo(attrvo);

        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存  需要将接受pojo转化为vo类 这样既能够实现数据库数据导入，又能实现逻辑关系处理
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody Attrvo attrvo){
//		attrService.save(attr);
        attrService.saveAndRelation(attrvo);

        return R.ok();
    }

    /**
     * 修改分组时，分组与分类的联系也要变
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody Attrvo attrvo){
        attrService.updatePaths(attrvo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }


}
